<?php
namespace Com\Pay;

//通联微信公众号支付

class TonglianPay{

    private $config = array();

    public function __construct($mchConfig,$config = null){
        $this->config = $mchConfig;
    }

    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        /* 获取提交的订单交易金额 */
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/TonglianNotifyGzh';
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            "cusid" => $this->config['mch_id'],
            "appid" => $this->config['appid'],
            "trxamt" => $price * 100,
            "acct"   => $order_info['openid'],
            "reqsn" => $order_sn,
            "paytype" => 'W02', // ﻿W02
            "randomstr" => $this->getNonceStr(13), // 单位：分
            "notify_url" => $notifyUrl,
        );
        $sign = $this->setSign($post_data,$this->config['mch_key'],false);
        $post_data['sign'] = $sign;
        $tar_url = 'https://vsp.allinpay.com/apiweb/unitorder/pay';
        $res = json_decode($this->https_request($tar_url,$post_data),true);
//        dump($res);die;
        if($res['trxstatus'] == '0000'){
            return json_decode($res['payinfo'],true);
        }
        return false;
    }


    // 生成签名方法
    private function setSign($param,$key,$IS_Notify){
        $str = "";
        $param['key'] = $key;
        ksort($param);
        foreach($param as $key1=>$val)  {
            if($IS_Notify && $key1 == 'sign') continue;
            $str.=$key1."=".$val."&";
        }
        $str = rtrim($str,'&');
        $sign = strtoupper(md5($str));
        return $sign;
    }


    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $Mysign = $this->setSign($data,$this->config['mch_key'],true);
        if($Mysign === $data['sign']){
            if($data['trxstatus'] == '0000'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }

    function getNonceStr($length = 32)
    {
        $chars = "0123456789abcdef";
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return str_shuffle($str);
    }


    function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }



}
